<?php

require_once('index.php');

define('PUBLIC_DIR',dirname(__FILE__).'/public');
define('IMG_DIR',PUBLIC_DIR.'/images');
define('IMG_PATH','public/images');

require_once('config.php');
require_once('admin/config/myconfig.php');
require_once('database.php');
require_once('admin/libraries/thumb/ThumbLib.inc.php');
if(empty($_FILES['Filedata']['name'])) die;

$arr_filename = explode('.', $_FILES['Filedata']['name']);
$extname = $arr_filename[count($arr_filename)-1];
$extname = strtoupper($extname);

if(!in_array($extname, array('JPG','GIF','PNG'))) die;

$rname = rand_filename();
$ofname = 'O_'.$rname.'.'.$extname;
$tfname = 'B_'.$rname.'.'.$extname;
$lfname = 'L_'.$rname.'.'.$extname;

$opath = IMG_PATH.'/'.$ofname;
$tpath = IMG_PATH.'/'.$tfname;
$lpath = IMG_PATH.'/'.$lfname;

$tempFile = $_FILES['Filedata']['tmp_name'];

$thumb = PhpThumbFactory::create($tempFile);
$thumb->resize($config['thumb']['w']);
$thumb->save($tpath);

$thumb = PhpThumbFactory::create($tempFile);
$thumb->adaptiveResize($config['thumb']['lw'], $config['thumb']['lh']);
$thumb->save($lpath);

@move_uploaded_file($tempFile, $opath);

$conn = mysql_connect($db['default']['hostname'],$db['default']['username'],$db['default']['password']);
mysql_select_db('xk',$conn);

$sql = "insert into images(o,t,l,atime) values(
	'".$opath."','".$tpath."','".$lpath."',".mktime()."
)";

mysql_query($sql);

$id = mysql_insert_id();

echo json_encode(array(
	'spath'=>$config['base_url'].$lpath,
	'id'=>$id
)); 

exit;

